﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JihankiLib;

namespace GuiJihanki
{
    /// <summary>
    /// 自販機商品棚ロジック
    /// </summary>
    class JihankiItemUnitBoxProcesser
    {
        private ItemStock stock;
        private JihankiItemBoxMachine machine;

        public int Count
        {
            get { return stock.Count; }
        }

        public int ItemValue
        {
            get { return stock.Value;}
        }

        public int ItemUnitNo
        {
            get { return stock.UnitNo; }
        }

        /// <summary>
        /// 入金額が商品購入可能金額に達した
        /// </summary>
        public event EventHandler MoneyStatusChanged_ReadyToBuy;

        /// <summary>
        /// 入金したお金が排出された
        /// </summary>
        public event EventHandler RejectedMoney;

        public JihankiItemUnitBoxProcesser(ItemStock stock, JihankiItemBoxMachine machine)
        {
            this.stock = stock;
            this.machine = machine;

            //ここでセットしちゃう
            for(var i=0;i<5;i++)
                stock.Add(new MessageDialogResult(string.Format("ユニット{0}のアイテム{1}です！！", stock.UnitNo, i+1)));

            machine.InMoneyStatusChanged += new EventHandler<InMoneyStatusChangedEventArgs>(machine_InMoneyStatusChanged);
        }

        void machine_InMoneyStatusChanged(object sender, InMoneyStatusChangedEventArgs e)
        {
            if (e.CurrentMoney == 0)
                if (RejectedMoney != null)
                    RejectedMoney(this, new EventArgs());

            if (stock.Value <= e.CurrentMoney)
                if(MoneyStatusChanged_ReadyToBuy != null)
                MoneyStatusChanged_ReadyToBuy(this, new EventArgs());
        }

        public void PurchaseItem()
        {
            machine.PurchaseItem(ItemUnitNo, ItemValue, stock);
        }

        public bool ExistStock()
        {
            return stock.ExistStock();
        }
    }
}
